// 社团管理部分接口
var express = require('express');
var date = require("silly-datetime");
var router = express.Router();
const {
    setResult
} = require("../util/utils");
var fs = require("fs");
const {
    sqlConnect
} = require('../sql/sqlUtil');
// 获取学校名
router.get('/school', (req, res) => {
    sqlConnect("SELECT id,s_name FROM school WHERE is_del=0", (err, data) => {
        if (err) {
            return console.log(err)
        }
        res.send(setResult(200, "success", data));
    })
})
// 查询社团信息|模糊查询
router.get('/', function (req, res, next) {
    try {
        let page = req.query.page - 1,
            limit = req.query.limit * 1,
            begin = page * limit,
            role = req.query.role,
            schools = req.query.schools;
        if (req.query.searchParams == undefined) {
            // 判断是员工还是管理员
            let sqlCount = null,
                sql = null;
            if (role == 0) {
                schools = '"' + schools.split(",").join('","') + '"';
                // schools=schools.split(",").join('","');
                sqlCount = `select count(1) as count from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name in (${schools})`;
                sql = `select a.id,a.sid,a.name,b.s_name from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name in (${schools}) order by 1 desc limit ?,?`;
            } else {
                sqlCount = `select count(1) as count from club as a inner join school as b on a.sid=b.id and a.is_del=0`;
                sql = `select a.id,a.sid,a.name,b.s_name from club as a inner join school as b on a.sid=b.id and a.is_del=0 order by 1 desc limit ?,?`;
            }
            // 获取数据总数
            sqlConnect(sqlCount, (err, data) => {
                if (err) {
                    console.log(err);
                } else {
                    var count = data[0].count;
                    sqlConnect(sql, [begin, limit], function (err, data) {
                        if (err) {
                            console.log(err);
                        } else {
                            var json = setResult();
                            json.code = 0;
                            json.count = count;
                            json.data = data;
                            res.send(json);
                        }
                    })
                }
            })
        } else {
            var result = JSON.parse(req.query.searchParams);
            var name = (result.name.length === 0) ? '%' : result.name,
                s_name = (result.s_name.length === 0) ? '%' : result.s_name;
                console.log(result);
                console.log(name);
                console.log(s_name);
            // 判断是员工还是管理员
            let sqlCount = null,
                sql = null;
            if (role == 0) {
                schools = '"' + schools.split(",").join('","') + '"';
                // schools=schools.split(",").join('","');
                sqlCount = `select count(1) as count from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name like '%${s_name}%' and a.name like '%${name}%' and b.s_name in (${schools})`;
                sql = `select a.id,a.sid,a.name,b.s_name from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name like '%${s_name}%' and a.name like '%${name}%' and b.s_name in (${schools}) order by 1 desc limit ?,?`;
            } else {
                sqlCount = `select count(1) as count from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name like '%${s_name}%' and a.name like '%${name}%'`;
                sql = `select a.id,a.sid,a.name,b.s_name from club as a inner join school as b on a.sid=b.id and a.is_del=0 and b.s_name like '%${s_name}%' and a.name like '%${name}%' order by 1 desc limit ?,?`;
            }
            // 计算表中符合条件信息数量
            sqlConnect(sqlCount, (err, data) => {
                if (err) {
                    console.log(err);
                } else {
                    var count = data.length;
                    sqlConnect(sql, [begin, limit], function (err, data) {
                        if (err) {
                            console.log(err)
                        } else {
                            var json = setResult();
                            json.code = 0;
                            json.count = count;
                            json.data = data;
                            res.send(json);
                        }
                    })
                }
            })
        }
    } catch (err) {
        console.log(err);
    }

});
// 添加社团
router.post('/', (req, res) => {
    const id = req.body.id,
        name = req.body.name;
    sqlConnect("select * from club where name=? and sid=? and is_del=0", [name, id], (err, data) => {
        if (err) {
            console.log(err);
            return res.send(setResult(500))
        }
        if (data.length > 0) {
            return res.send(setResult(201));
        }
        return sqlConnect("INSERT INTO club (name,sid,is_del) VALUES (?,?,0)", [name, id], (err, data) => {
            if (err) {
                console.log(err);
                return res.send(setResult(500));
            }
            res.send(setResult());
        })
    })

})
// 修改社团信息
router.put('/', (req, res) => {
    console.log(req.body);
    const id = req.body.id,
        name = req.body.name,
        sid = req.body.sid;
    sqlConnect("update club set name=?,sid=? where id=?", [name, sid, id], (err, data) => {
        if (err) {
            console.log(err);
            return res.send(setResult(500))
        }
        res.send(setResult());
    })

})
// 删除社团信息
router.delete("/", function (req, res, next) {
    const id = req.body.id;
    let sql = `update club set is_del=1 where id in(${id})`;
    sqlConnect(sql, id, (err, data) => {
        if (err) {
            res.send(setResult());
        } else {
            var json = setResult();
            json.msg = "删除成功";
            res.send(json);
        }
    })
})
module.exports = router;